<?php
namespace Common\Model\Admin;

use Common\Base\ModelBase;

class GroupModel extends ModelBase
{

	/**
     * 得到分组列表
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getGroupList($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='', $exclude='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['group_id']) && intval($search['group_id'])>0 && $where .= ' and group_id='.intval($search['group_id']);
            isset($search['st']) && $search['st']!='' && $where .= ' and st='.intval($search['st']);
            isset($search['group_name']) && $search['group_name']!='' && $where .= " and group_name='".$search['group_name']."'";
            isset($search['group_path']) && $search['group_path']!='' && $where .= " and group_path like '%".$search['group_path']."%'";
        }
        if($exclude){ $where .= $exclude; }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'group_id asc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(group_id) as tot from '.getTable('acl_group').$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('acl_group').$where.$orderBy;

        return $this->getData($w, $sql, $page, $pageSize);
    }

    /**
     * 添加分组
     *
     * @param array $dat
     *
     * @return mixed
     */
    public function addGroup($dat = array())
    {
        return $this->addRow($dat, getTable('acl_group'));
    }

    /**
     * 修改分组
     *
     * @param array $dat 需要更新的数据
     * @param int   $group_id
     *
     * @return mixed
     */
    public function updateGroupByGroupId($dat=array(), $group_id=0)
    {
        return $this->updateRow(array('group_id'=>$group_id), $dat, getTable('acl_group'));
    }

    /**
     * 删除分组
     *
     * @param int $group_id
     *
     * @return mixed
     */
    public function delGroup($group_id = 0)
    {
        return $this->delRow(array('group_id'=>$group_id), getTable('acl_group'));
    }

}